//******************************************************************************
//this code is protected by the GNU affero GPLv3
//author:Sylvain BERTRAND <sylvain.bertrand AT gmail dot com>
//                        <digital.ragnarok AT gmail dot com>
//******************************************************************************
#include "globals.h"

static s8 create_buffer(struct client *c,s32 shm_pool_slot,struct msg *m)
{
  s8 r;
  if(m->req_dws<8){
    PERR("client(%d):fatal:shm_pool::create_buffer missing data\n",c->so);
    r=LWL_ERR;goto exit;
  }

  LOG_WIRE("client(%d):shm_pool::create_buffer shm_pool_id=%u"
                          " shm_pool_buffer_id=%u offset=%d w=%d h=%d stride=%d"
                                          " fmt=%u\n",c->so,m->req[0],m->req[2],
                                                  (s32)m->req[3],(s32)m->req[4],
                                                  (s32)m->req[5],(s32)m->req[6],
                                                                     m->req[7]);

  r=shm_pool_create_buffer(c,shm_pool_slot,m->req[2],(s32)m->req[3],
                        (s32)m->req[4],(s32)m->req[5],(s32)m->req[6],m->req[7]);
exit:
  return r;
}

static s8 resize(struct client *c,s32 shm_pool_slot,struct msg *m)
{
  s8 r;
  if(m->req_dws<3){
    PERR("client(%d):fatal:shm_pool::resize missing size\n",c->so);
    r=LWL_ERR;goto exit;
  }

  LOG_WIRE("client(%d):shm_pool::resize shm_pool_id=%u sz=%d\n",c->so,m->req[0],
                                                                (s32)m->req[2]);

  r=shm_pool_resize(c,shm_pool_slot,(s32)m->req[2]);
exit:
  return r;
}

s8 wl_shm_pool_req(struct client *c,s32 shm_pool_slot,struct msg *m)
{
  s8 r;
  switch(m->req_op){
  case WL_SHM_POOL_CREATE_BUFFER:
    r=create_buffer(c,shm_pool_slot,m);
    break;
  case WL_SHM_POOL_DESTROY:
    LOG_WIRE("client(%d):shm_pool::destroy shm_pool_id=%u\n",c->so,m->req[0]);
    r=shm_pool_destroy(c,shm_pool_slot);
    break;
  case WL_SHM_POOL_RESIZE:
    r=resize(c,shm_pool_slot,m);
    break;
  default:
    PERR("client(%d):fatal:shm_pool unknown opcode %u\n",c->so,m->req_op);
    r=LWL_ERR;
  };
  return r;
}
